<template>
<div>
  <div class="app-container">
<div style="width: 200px">
    <el-input v-model="input" placeholder="请输入内容" show-password prefix-icon="el-icon-phone"></el-input>
    <el-input v-model="input" placeholder="请输入内容" show-password suffix-icon="el-icon-s-goods"></el-input>
    <el-input v-model="input" placeholder="请输入内容" show-password></el-input>
</div>
    <el-button @click="click2" size="medium">点击修改输入框内容</el-button>

    <el-radio v-model="radio" label="1" @change="change">选项1</el-radio>
    <el-radio v-model="radio" label="2" @change="change">选项2</el-radio>

    <i class="el-icon-edit"></i>
    <el-button type="primary" icon="el-icon-search">搜索</el-button>
    <i class="el-icon-eleme"></i>
    <i class="el-icon-share"></i>
    <el-link href="www.baidu.com" type="primary" target="_blank">主要链接</el-link>
    <el-row>
      <el-button>默认按钮</el-button>
      <el-button type="primary" circle></el-button>
      <el-button type="success">成功按钮</el-button>
      <el-button type="info">信息按钮</el-button>
      <el-button type="warning">警告按钮</el-button>
<!--      添加事件绑定-->
      <el-button type="danger" icon="el-icon-eleme" circle @click="test1()" >9</el-button>
      <el-button type="danger" icon="el-icon-eleme" circle @click="test1()" :loading="loading"></el-button>
      <el-button type="danger" circle :loading="loading"></el-button>
    </el-row>
    <el-row>
      <el-button type="danger" size="mini" circle>危险按钮666</el-button>
      <el-button type="danger" plain>危险按钮666</el-button>
      <el-button type="danger" disabled>危险按钮</el-button>
    </el-row>


    <!--    搜索表单——新闻名称-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="新闻名称" prop="newsName">
        <el-input
          v-model="queryParams.newsName"
          placeholder="请输入新闻名称111"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--    搜索表单——新闻名称-->
      <el-form-item label="新闻分类" prop="newsCategory">
        <el-select v-model="queryParams.newsCategory" placeholder="请选择新闻分类" clearable>
          <!--下拉展示信息label、选择完提交的为value-->
          <el-option
            v-for="dict in dict.type.news_sort"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--===========================================================================================-->
      <el-form-item label="新闻状态" prop="newsStatus">
        <el-select v-model="queryParams.newsCategory" placeholder="请选择新闻状态" clearable>
          <el-option
            v-for="status in newsStatusOptions"
            :key="status.value"
            :label="status.label"
            :value="status.value"
          />
        </el-select>
      </el-form-item>
<!--================================================================================================-->
<!-- 搜索按钮-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


<!-- 新增、修改等   按钮区域=====================================================================-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['news:news:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['news:news:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['news:news:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['news:news:export']"
        >导出</el-button>
      </el-col>
<!--      自定义组件：隐藏搜索+刷新页面-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>



<!---------据表格主体页面-->
<!--    loading加载、data绑定属性，指定数据源、时间监听器-->
    <el-table v-loading="loading" :data="newsList" @selection-change="handleSelectionChange">
<!--      selection复选框-->
      <el-table-column type="selection" width="55" align="center" />
<!--具体数据源内容，label列表名-->
      <el-table-column label="新闻ID" align="center" prop="id" />
      <el-table-column label="新闻名称" align="center" prop="newsName" />
<!--      存储的数据字典的值，展示字典标签-->
      <el-table-column label="新闻分类" align="center" prop="newsCategory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.news_sort" :value="scope.row.newsCategory"/>
        </template>
      </el-table-column>


      <el-table-column label="新闻状态" align="center" prop="newsStatus" >
        <template slot-scope="scope">
          <template v-for="status in newsStatusOptions">
              <div v-if="status.value === scope.row.newsStatus">{{status.label}}</div>
          </template>
        </template>
      </el-table-column>

      <el-table-column label="新闻介绍" align="center" prop="newsIntroduction" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['news:news:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['news:news:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>


<!-- ++++++++++++++++++++++++++++++分页栏+++++++++++++++++++++++++++++++++++++++++++++++++-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改新闻管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="新闻名称" prop="newsName">
          <el-input v-model="form.newsName" placeholder="请输入新闻名称" />
        </el-form-item>
        <el-form-item label="新闻分类" prop="newsCategory">
          <el-select v-model="form.newsCategory" placeholder="请选择新闻分类">
            <el-option
              v-for="dict in dict.type.news_sort"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="新闻状态" prop="newsStatus">
          <el-select v-model="form.newsStatus" placeholder="请选择新闻状态">
            <el-option
              v-for="item in newsStatusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="新闻介绍" prop="newsIntroduction">
          <el-input v-model="form.newsIntroduction" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
  <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
  <div style="margin: 15px 0;"></div>
  <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
    <el-checkbox v-for="city in cities" :label="city" :key="city">{{city}}</el-checkbox>
  </el-checkbox-group>



  <el-select v-model="value" placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value"
      :disabled="item.disable"
    >
    </el-option>
  </el-select>
  <el-button @click="click3">点击打印内容</el-button>

  <el-switch
    v-model="vvv"
    active-color="#13ce66"
    inactive-color="#ff4949">
  </el-switch>
  <el-upload
    class="upload-demo"
    drag
    action="https://jsonplaceholder.typicode.com/posts/"
    multiple>
    <i class="el-icon-upload"></i>
    <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
    <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
  </el-upload>

  <el-form ref="form" :model="form" label-width="80px">
    <el-form-item label="活动名称">
      <el-input v-model="form.name"></el-input>
    </el-form-item>
    <el-form-item label="活动区域">
      <el-select v-model="form.region" placeholder="请选择活动区域">
        <el-option label="区域一" value="shanghai"></el-option>
        <el-option label="区域二" value="beijing"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="活动时间">
      <el-col :span="11">
        <el-date-picker type="date" placeholder="选择日期" v-model="form.date1" style="width: 100%;"></el-date-picker>
      </el-col>
      <el-col class="line" :span="2">-</el-col>
      <el-col :span="11">
        <el-time-picker placeholder="选择时间" v-model="form.date2" style="width: 100%;"></el-time-picker>
      </el-col>
    </el-form-item>
    <el-form-item label="即时配送">
      <el-switch v-model="form.delivery"></el-switch>
    </el-form-item>
    <el-form-item label="活动性质">
      <el-checkbox-group v-model="form.type">
        <el-checkbox label="美食/餐厅线上活动" name="type"></el-checkbox>
        <el-checkbox label="地推活动" name="type"></el-checkbox>
        <el-checkbox label="线下主题活动" name="type"></el-checkbox>
        <el-checkbox label="单纯品牌曝光" name="type"></el-checkbox>
      </el-checkbox-group>
    </el-form-item>
    <el-form-item label="特殊资源">
      <el-radio-group v-model="form.resource">
        <el-radio label="线上品牌商赞助"></el-radio>
        <el-radio label="线下场地免费"></el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="活动形式">
      <el-input type="textarea" v-model="form.desc"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">立即创建</el-button>
      <el-button>取消</el-button>
    </el-form-item>
  </el-form>


  <el-table
    :data="tableData"
    border
    style="width: 800px">
    <el-table-column
      fixed
      prop="date"
      label="日期"
      width="150">
    </el-table-column>
    <el-table-column
      prop="name"
      label="姓名"
      width="120">
    </el-table-column>
    <el-table-column
      prop="province"
      label="省份"
      width="120">
    </el-table-column>

    <el-table-column
      fixed="right"
      label="操作"
      width="300">
      <template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

</div>

</template>





<script>
import { listNews, getNews, delNews, addNews, updateNews, getNewsStatus } from "@/api/news/news"
const cityOptions = ['上海', '北京', '广州', '深圳'];

export default {
  name: "News",
  dicts: ['news_sort'],
  data() {
    return {

      tableData: [{
        date: '2016-05-02',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1518 弄',
        zip: 200333
      }, {
        date: '2016-05-04',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1517 弄',
        zip: 200333
      }, {
        date: '2016-05-01',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1519 弄',
        zip: 200333
      }, {
        date: '2016-05-03',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1516 弄',
        zip: 200333
      }],

      vvv:true,
      input: '',
      checkAll: false,
      checkedCities: ['上海', '北京'],
      cities: cityOptions,
      isIndeterminate: true,

      radio: "1",
      options: [{
        value: '选项1',
        label: '黄金糕',
        disable:true
      }, {
        value: '选项2',
        label: '双皮奶'
      }, {
        value: '选项3',
        label: '蚵仔煎'
      }, {
        value: '选项4',
        label: '龙须面'
      }, {
        value: '选项5',
        label: '北京烤鸭'
      }],
      // value为默认显示值
      value: '',




      newsStatusOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 新闻管理表格数据
      newsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        newsName: null,
        newsCategory: null,
        newsStatus: null,
        newsIntroduction: null,

      },
      // 表单参数
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      // 表单校验
      rules: {
        newsName: [
          { required: true, message: "新闻名称不能为空", trigger: "blur" }
        ],
        newsCategory: [
          { required: true, message: "新闻分类不能为空", trigger: "change" }
        ],
      }
    }
  },
  created() {
    this.getList()
    this.getNewsStatus()
  },
  // 方法
  methods: {
    handleEdit(index, row) {
      console.log(index, row);
    },
    // handleDelete(index, row) {
    //   console.log(index, row);
    // },
    onSubmit() {
      console.log(this.form);
    },
    click3(){
      console.log(this.value)
    },
    click2(){
      this.input = 'aaa'
    },
    handleCheckAllChange(val) {
      this.checkedCities = val ? cityOptions : [];
      this.isIndeterminate = false;
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.cities.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length;
    },


    change(){
      console.log("当前radio的值为：" + this.radio)
    },
    test1(){
      alert(1)
      this.loading = false;
      // setTimeout(()=>{
      //     this.loading = true;
      //   },3000
      // )
    },
    /** 查询新闻管理列表 */
    getList() {
      this.loading = true
      listNews(this.queryParams).then(response => {
        this.newsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getNewsStatus(){
      this.loading = true;
      getNewsStatus().then(response => {
        this.newsStatusOptions = response.data; // 正确接收状态列表
        this.loading = false;
      }).catch(error => {
        this.loading = false;
        this.$message.error("获取新闻状态失败：" + error.msg); // 错误提示
      });
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        newsName: null,
        newsCategory: null,
        newsStatus: null,
        newsIntroduction: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加新闻管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getNews(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改新闻管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNews(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addNews(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除新闻管理编号为"' + ids + '"的数据项？').then(function() {
        return delNews(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('news/news/export', {
        ...this.queryParams
      }, `news_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
